﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Security.Principal;
using System.Web.Security;
using Vmago.Models.DBML;
using Vmago.Models.DAL;
using System.IO;
using System.Collections;
using Vmago.Models;
using System.Text.RegularExpressions;
using System.Text;

namespace Vmago.Controllers
{
    public class TrademarkInformationController : ControllerBase
    {
        #region Global：Member

        /// <summary>共通方法</summary>
        private ComDAL m_dalCom = new ComDAL();

        /// <summary>TblDataContext对象</summary>
        private TblDataContext m_dcTbl = new TblDataContext();

        /// <summary>共通Controller</summary>
        private UtilController m_cUtil = new UtilController();

        /// <summary>PrcDataContext对象</summary>
        private PrcDataContext m_dcPrc = new PrcDataContext();

        #endregion

        #region Server：Get：Register

        public ActionResult Register()
        {
            return m_cUtil.ViewUtil("Register", this);
        }

        #endregion

        #region Ajax：Post：Register_Step1
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Register_Step1(FormCollection oForm)
        {
            if (Membership.GetUser(oForm["R.Username"]) != null) { return null; }

            ViewData["R.ShopCategory"] = m_dalCom.GetShopCategory("0");

            ViewData["R.GeoCountry"] = m_dalCom.GetGeoCountry();

            ViewData["R.GeoProvince"] = m_dalCom.GetGeoProvince();

            ViewData["R.GeoCity"] = m_dalCom.GetGeoCity();

            ViewData["R.GeoSuburb"] = m_dalCom.GetGeoSuburb();

            Session["T.R.UserMembership"] = new UserMembership
            {
                User = new User { UserName = oForm["R.Username"] },
                Password = oForm["R.Password"],
                Email = oForm["R.Email"]
            };

            return PartialView("Register_Step2");
        }

        #endregion

        #region Ajax：Post：Register_Step2

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Register_Step2(FormCollection oForm)
        {
            Int32 intCategoryID = 0;
            Int32 intCountryID = 0;
            Int32 intProvinceID = 0;
            Int32 intCityID = 0;
            Int32 intSuburbID = 0;
            Int32.TryParse(oForm["R.ShopCategory"],out intCategoryID);
            Int32.TryParse(oForm["R.GeoCountry"], out intCountryID);
            Int32.TryParse(oForm["R.GeoProvince"], out intProvinceID);
            Int32.TryParse(oForm["R.GeoCity"], out intCityID);
            Int32.TryParse(oForm["R.GeoSuburb"], out intSuburbID);

            List<tbl_shopCategoryItem> oAttributes = (from p in m_dcTbl.tbl_shopCategoryItemMapping
                                                      from o in m_dcTbl.tbl_shopCategoryItem
                                                      where p.categoryID == intCategoryID
                                                      where p.itemID == o.itemID
                                                      select o).ToList();

            Session["T.R.tbl_shopPosition"] = new tbl_shopPosition
            {
                city = intCountryID,
                country = intProvinceID,
                province = intCityID,
                suburb = intSuburbID
            };

            Session["T.R.tbl_shop"] = new tbl_shop { name = oForm["Shopname"] };
             
            return PartialView("Register_Step3", oAttributes);
        }

        #endregion

        #region Ajax：Post：Register_Step3

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Register_Step3(FormCollection oForm)
        {
            return PartialView("Register_Step4");
        }

        #endregion

        #region Ajax：Post：Register_Step4

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Register_Step4(FormCollection oForm)
        {
            return PartialView("Register_Step5");
        }

        #endregion

        #region Register：Register_AddShop

        #region 公有方法：提交处理

        /// <summary>
        /// Register_AddShop.asxc提交处理
        /// </summary>
        /// <param name="Image"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Register_AddShop(HttpPostedFileBase Image, String RegUsername, String RegEmail, String RegPassword, String RegConfirmPassword)
        {
            ViewData["ShopCategory"] = new SelectList(m_dalCom.GetShopCategory(), "categoryID", "categoryName");

            //添加用户Check
            if (String.IsNullOrEmpty(RegUsername))
            {
                ViewData.ModelState.AddModelError("VMRegUsername", "用户名不可为空");
            }
            if (String.IsNullOrEmpty(RegEmail))
            {
                ViewData.ModelState.AddModelError("VMRegEmail", "电子邮箱不可为空");
            }
            if (String.IsNullOrEmpty(RegPassword))
            {
                ViewData.ModelState.AddModelError("VMRegPassword", "密码不可为空");
            }
            if (String.IsNullOrEmpty(RegConfirmPassword))
            {
                ViewData.ModelState.AddModelError("VMRegConfirmPassword", "密码不可为空");
            }
            if (!String.Equals(RegConfirmPassword, RegPassword))
            {
                ViewData.ModelState.AddModelError("VMRegConfirmPassword", "两次输入密码不相同");
            }
            //添加商铺信息Check
            if (String.IsNullOrEmpty(Request.Params["ShopName"]))
            {
                ViewData.ModelState.AddModelError("VMShopName", "商铺名称不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["CHName"]))
            {
                ViewData.ModelState.AddModelError("VMCHName", "中文名称不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["JPName"]))
            {
                ViewData.ModelState.AddModelError("VMJPName", "日文名称不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["ENName"]))
            {
                ViewData.ModelState.AddModelError("VMENName", "英文名称不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["City"]))
            {
                ViewData.ModelState.AddModelError("VMCity", "城市不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["Hours"]))
            {
                ViewData.ModelState.AddModelError("VMHours", "营业时间不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["Holiday"]))
            {
                ViewData.ModelState.AddModelError("VMHoliday", "休息日不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["Average"]))
            {
                ViewData.ModelState.AddModelError("VMAverage", "平均消费不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["Phone"]))
            {
                ViewData.ModelState.AddModelError("VMPhone", "联系电话不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["Tel"]))
            {
                ViewData.ModelState.AddModelError("VMTel", "定位电话不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["Fax"]))
            {
                ViewData.ModelState.AddModelError("VMFax", "传真不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["Web"]))
            {
                ViewData.ModelState.AddModelError("VMWeb", "官方网站不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["ImageAlt"]))
            {
                ViewData.ModelState.AddModelError("VMImageAlt", "图片描述不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["Address"]))
            {
                ViewData.ModelState.AddModelError("VMAddress", "地址不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["GPS"]))
            {
                ViewData.ModelState.AddModelError("VMGPS", "定位地址不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["Traffic"]))
            {
                ViewData.ModelState.AddModelError("VMTraffic", "市区间交通不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["Special"]))
            {
                ViewData.ModelState.AddModelError("VMSpecial", "特别设施不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["Seats"]))
            {
                ViewData.ModelState.AddModelError("VMSeats", "席位不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["Language"]))
            {
                ViewData.ModelState.AddModelError("VMLanguage", "语言不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["CreditCard"]))
            {
                ViewData.ModelState.AddModelError("VMCreditCard", "信用卡不可为空");
            }
            if (String.IsNullOrEmpty(Request.Params["Descrition"]))
            {
                ViewData.ModelState.AddModelError("VMDescrition", "基本介绍不可为空");
            }


            if (!ViewData.ModelState.IsValid)
            {
                ViewData["RegUsername"] = Request.Params["RegUsername"];
                ViewData["RegEmail"] = Request.Params["RegEmail"];
                ViewData["RegPassword"] = Request.Params["RegPassword"];
                ViewData["RegConfirmPassword"] = Request.Params["RegConfirmPassword"];

                ViewData["ShopName"] = Request.Params["ShopName"];
                ViewData["CHName"] = Request.Params["CHName"];
                ViewData["JPName"] = Request.Params["JPName"];
                ViewData["ENName"] = Request.Params["ENName"];
                ViewData["Category"] = Request.Params["Category"];
                ViewData["Web"] = Request.Params["Web"];
                ViewData["Traffic"] = Request.Params["Traffic"];
                ViewData["Hours"] = Request.Params["Hours"];
                ViewData["Phone"] = Request.Params["Phone"];
                ViewData["Descrition"] = Request.Params["Descrition"];
                ViewData["Tickets"] = Request.Params["Tickets"];
                ViewData["Tel"] = Request.Params["Tel"];
                ViewData["Fax"] = Request.Params["Fax"];
                ViewData["Holiday"] = Request.Params["Holiday"];
                ViewData["Special"] = Request.Params["Special"];
                ViewData["Seats"] = Request.Params["Seats"];
                ViewData["Language"] = Request.Params["Language"];
                ViewData["CreditCard"] = Request.Params["CreditCard"];
                ViewData["Average"] = Request.Params["Average"];
                ViewData["GPS"] = Request.Params["GPS"];
                return View("Register");
            }

            Guid UserID = Register_AddUser(RegUsername, RegEmail, RegPassword, RegConfirmPassword);

            //新建商铺
            int? inShopID = Register_AddShop_InsertShop(UserID);

            //新建失败处理
            if (inShopID == 0) { return Register(); }

            //新建商品信息
            Register_AddShop_InsertShopInfo(inShopID.Value);

            //新建商铺Logo
            Register_AddShop_InsertShopImage(inShopID.Value, Image);

            //转向产品新建目录
            return Register();
        }

        #endregion

        #region 私有方法：新建商铺，商铺位置

        /// <summary>
        /// 新建商铺，商铺位置
        /// </summary>
        /// <returns></returns>
        private int? Register_AddShop_InsertShop(Guid userid)
        {
            int? rtnShopID = 0;

            //初始化TblDataContext对象
            m_dcTbl = new TblDataContext();

            //初始化tbl_shop对象
            tbl_shop oShop = new tbl_shop();
            oShop.name = Request.Params["ShopName"];
            oShop.address = Request.Params["Address"];
            oShop.description = Request.Params["Descrition"];
            oShop.parentShopID = 0;
            oShop.createDate = DateTime.Now;

            //初始化tbl_shopPosition对象
            tbl_shopPosition oPosition = new tbl_shopPosition();
            oPosition.address = Request.Params["GPS"];

            //m_dcTbl.prc_shopCreateShop(null,
            //                            oShop.name,
            //                            oPosition.country,
            //                            oPosition.province,
            //                            oPosition.city,
            //                            oPosition.suburb,
            //                            oPosition.street,
            //                            oPosition.postcode,
            //                            oPosition.address,
            //                            oShop.address,
            //                            oPosition.latitude,
            //                            oPosition.longitude,
            //                            oShop.parentShopID,
            //                            oShop.locatedParentShopID,
            //                            oShop.description,
            //                            userid,
            //                            ref rtnShopID);

            return rtnShopID;
        }

        #endregion

        #region 私有方法：新建商铺信息，商铺信息关联

        /// <summary>
        /// 新建商品信息，商铺信息关联
        /// </summary>
        /// <param name="intShopID"></param>
        private void Register_AddShop_InsertShopInfo(int intShopID)
        {
            //初始化TblDataContext对象
            m_dcTbl = new TblDataContext();

            Int32 intCategory = 0;
            Int32.TryParse(Request.Params["Category"], out intCategory);

            //初始化tbl_shopCategoryMappingValue对象集
            List<tbl_shopCategoryMappingValue> oValue = new List<tbl_shopCategoryMappingValue>();
            for (int i = 0; i < 19; i++) { oValue.Add(new tbl_shopCategoryMappingValue()); }
            oValue[0].value = Request.Params["CHName"];
            oValue[1].value = Request.Params["JPName"];
            oValue[2].value = Request.Params["ENName"];
            oValue[3].value = Request.Params["Category"];
            oValue[4].value = Request.Params["Web"];
            oValue[5].value = Request.Params["Traffic"];
            oValue[6].value = Request.Params["Hours"];
            oValue[7].value = Request.Params["Phone"];
            oValue[8].value = Request.Params["Descrition"];
            oValue[9].value = Request.Params["Tickets"];
            oValue[10].value = Request.Params["Tel"];
            oValue[11].value = Request.Params["Fax"];
            oValue[12].value = Request.Params["Holiday"];
            oValue[13].value = Request.Params["Special"];
            oValue[14].value = Request.Params["Seats"];
            oValue[15].value = Request.Params["Language"];
            oValue[16].value = Request.Params["CreditCard"];
            oValue[17].value = Request.Params["Average"];
            oValue[18].value = Request.Params["GPS"];
            for (Int32 i = 0; i < oValue.Count; i++)
            {
                oValue[i].categoryID = intCategory;
                oValue[i].itemID = i + 1;
                oValue[i].shopID = intShopID;
                m_dcTbl.tbl_shopCategoryMappingValue.InsertOnSubmit(oValue[i]);
            }

            //初始化tbl_shopCategoryMapping对象
            tbl_shopCategoryMapping oCateMapp = new tbl_shopCategoryMapping();
            oCateMapp.shopID = intShopID;
            oCateMapp.categoryID = intCategory;
            m_dcTbl.tbl_shopCategoryMapping.InsertOnSubmit(oCateMapp);

            //执行事务
            m_dcTbl.SubmitChanges();
        }

        #endregion

        #region 私有方法：新建商铺Logo

        /// <summary>
        /// 新建商铺Logo
        /// </summary>
        /// <param name="intShopID"></param>
        /// <param name="Image"></param>
        private void Register_AddShop_InsertShopImage(int intShopID, HttpPostedFileBase Image)
        {
            if (Image == null) { return; }

            m_dcTbl = new TblDataContext();

            //生成图片名
            String strImageFileName = String.Format("{0}.{1}", intShopID, Image.FileName.Split('.').Last());

            //生成图片路径
            String strPath = String.Format("{0}Content\\img\\TrademarkLogo\\{1}",
                                            AppDomain.CurrentDomain.BaseDirectory,
                                            strImageFileName);

            //保存图片
            Image.SaveAs(strPath);

            //初始化tbl_shopImage对象
            tbl_shopImage oImage = new tbl_shopImage();
            oImage.description = Request.Params["ImageAlt"];
            oImage.filename = strImageFileName;
            oImage.shopID = intShopID;
            oImage.type = 1;

            m_dcTbl.tbl_shopImage.InsertOnSubmit(oImage);

            //执行事务
            m_dcTbl.SubmitChanges();
        }

        #endregion

        #region 私有方法：新建商户用户

        private Guid Register_AddUser(String RegUsername, String RegEmail, String RegPassword, String RegConfirmPassword)
        {
            try
            {
                MembershipCreateStatus status;
                MembershipUser NewUser = Membership.CreateUser(RegUsername, RegPassword, RegEmail, null, null, true, out status);
                Roles.RemoveUserFromRole(NewUser.UserName, "consumerNormal");
                Roles.AddUserToRole(NewUser.UserName, "coporateNormal");

                return (String.Equals(status.ToString(), "Success")) ? (Guid)NewUser.ProviderUserKey : Guid.Empty;
            }
            catch (Exception)
            {
                return Guid.Empty;
            }

        }

        #endregion

        #endregion

        #region Register：Register_AddProduct

        #region 公有方法：提交处理

        /// <summary>
        /// Register_AddProduct.asxc提交处理
        /// </summary>
        /// <param name="Image"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Register_AddProduct(HttpPostedFileBase Image)
        {
            //新建商铺
            int? inShopID = 0;//Register_AddShop_InsertShop();

            //新建失败处理
            if (inShopID == 0) { return Register(); }

            //新建商品信息
            Register_AddShop_InsertShopInfo(inShopID.Value);

            //新建商铺Logo
            Register_AddShop_InsertShopImage(inShopID.Value, Image);

            //转向产品新建目录
            return Register();
        }

        #endregion

        #region 私有方法：新建商品

        private void Register_AddProduct_InsertProduct()
        {
            //Int32 intGrade = 0;
            //Int32 intProductType = 0;
            //Int32 intShopID = 0;

            //Int32.TryParse(Request.Params[""],out intGrade);
            //Int32.TryParse(Request.Params[""],out intProductType);
            //Int32.TryParse(Request.Params[""],out intShopID);

            //tbl_shopProduct oProduct = new tbl_shopProduct();
            //oProduct.description = Request.Params[""];
            //oProduct.grade = Int32.TryParse(Request.Params[""], out intGrade) ? intGrade : null;
            //oProduct.Language = String.Empty;
            //oProduct.productName = Request.Params[""];
            //oProduct.productType = intProductType;
            //oProduct.shopID= intShopID;
        }

        #endregion

        #region 私有方法：新建商品Logo

        /// <summary>
        /// 新建商品Logo
        /// </summary>
        /// <param name="intShopID"></param>
        /// <param name="Image"></param>
        private void Register_AddProduct_InsertShopImage(int intShopID, HttpPostedFileBase Image)
        {
            if (Image == null) { return; }

            m_dcTbl = new TblDataContext();

            //生成图片名
            String strImageFileName = String.Format("{0}.{1}", intShopID, Image.FileName.Split('.').Last());

            //生成图片路径
            String strPath = String.Format("{0}Content\\img\\TrademarkLogo\\{1}",
                                            AppDomain.CurrentDomain.BaseDirectory,
                                            strImageFileName);

            //保存图片
            Image.SaveAs(strPath);

            //初始化tbl_shopImage对象
            tbl_shopImage oImage = new tbl_shopImage();
            oImage.description = Request.Params["ImageAlt"];
            oImage.filename = strImageFileName;
            oImage.shopID = intShopID;
            oImage.type = 2;

            m_dcTbl.tbl_shopImage.InsertOnSubmit(oImage);

            //执行事务
            m_dcTbl.SubmitChanges();
        }

        #endregion

        #endregion

        #region Register：Register_Login

        #region 公共方法：登录并跳转

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Register_Login(string username, string password, bool rememberMe, string returnUrl, string Login)
        {
            if (Login.Equals("注册"))
            {
                return RedirectToAction("Register");
            }

            // 验证 用户名、密码是否为空
            if (String.IsNullOrEmpty(username) || username.Equals("Email或昵称"))
            {
                ModelState.AddModelError("username1", LoginGetErrMsg("username"));
                username = null;
            }
            if (String.IsNullOrEmpty(password))
            {
                ModelState.AddModelError("password1", LoginGetErrMsg("password"));
            }

            if (ViewData.ModelState.IsValid)
            {
                MembershipProvider Provider = Membership.Provider;
                // Attempt to login
                bool loginSuccessful = Provider.ValidateUser(username, password);

                if (loginSuccessful)
                {
                    MembershipUser NowUser = Membership.GetUser(username);

                    if (NowUser != null && NowUser.IsOnline)
                    {
                        Session["IsOnline"] = true;
                        UtilController u = new UtilController();
                        if (u.isShop(NowUser.ProviderUserKey))
                        {
                            Session["IsShop"] = true;
                        }
                        else
                        {
                            Session["IsShop"] = false;
                        }
                    }

                    IFormsAuthentication FormsAuth = new FormsAuthenticationWrapper();
                    FormsAuth.SetAuthCookie(username, rememberMe);
                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("Validation", LoginGetErrMsg("validation"));
                }
            }

            // If we got this far, something failed, redisplay form
            ViewData["rememberMe"] = rememberMe;

            return View("Control");
        }

        #endregion

        #region 私有方法：LoginGetErrMsg

        private string LoginGetErrMsg(string type)
        {
            string rtn = null;
            switch (type)
            {

                case "username":
                    rtn = "请输入正确的用户名";
                    break;
                case "password":
                    rtn = "请输入正确的密码";
                    break;
                case "validation":
                    rtn = "输入的用户名或者密码不正确";
                    break;
                default:
                    Console.WriteLine("Default case");
                    break;
            }
            return rtn;

        }

        #endregion

        #endregion

        #region Control：Init

        #region 公共方法：商铺管理初始化

        public ActionResult Control()
        {
            MembershipUser NowUser = Membership.GetUser();

            if (NowUser != null && NowUser.IsOnline)
            {
                Session["IsOnline"] = true;
                UtilController u = new UtilController();
                if (u.isShop(NowUser.ProviderUserKey))
                {
                    Session["IsShop"] = true;
                    List<tbl_shop> posts = Get_ShopList();
                    ViewData["fristshopid"] = posts[0].shopID;
                    ViewData["shoplist"] = posts;
                }
                else
                {
                    Session["IsShop"] = false;
                }
            }
            else
            {
                Session["IsOnline"] = false;
            }
            return View();
        }

        #region 私有方法：获取商铺列表

        /// <summary>
        /// 查询商铺信息
        /// </summary>
        /// <param name="intShopID"></param>
        private void Trademark_GetShop(Int32 intShopID)
        {
            //获得tbl_shop对象
            IEnumerable<tbl_shop> oShops = from o in m_dcTbl.tbl_shop
                                           where o.shopID == intShopID
                                           select o;

            if (oShops.Count() == 0)
            {
                ViewData["Name"] = String.Empty;
                ViewData["Address"] = String.Empty;
                ViewData["Descrition"] = String.Empty;
            }
            else
            {
                ViewData["shopid"] = intShopID;
                ViewData["Name"] = oShops.First().name;
                ViewData["Address"] = oShops.First().address;
                ViewData["Descrition"] = oShops.First().description;
            }
        }

        private List<tbl_shop> Get_ShopList()
        {
            MembershipUser user;
            user = Membership.GetUser(User.Identity.Name);
            Guid userid = (Guid)user.ProviderUserKey;

            var shop = new TblDataContext();
            var shoplist = from s in shop.tbl_shop
                           where s.userid == userid
                           orderby s.parentShopID
                           select s;

            if (shoplist.Count() != 0)
            {
                return shoplist.ToList<tbl_shop>();
            }
            else
            {
                return new List<tbl_shop>();
            }
        }

        #endregion

        #endregion

        #region 公共方法：编辑商户初始化

        public ActionResult Control_EditShop_Main()
        {
            String shopid = String.Empty;
            String ProductID = String.Empty;
            String mode = String.Empty;
            if (Request.QueryString["shopid"] != null && Request.QueryString["mode"] != null)
            {
                //获取商铺编号
                shopid = String.IsNullOrEmpty(Request.QueryString["shopid"].ToString()) ? String.Empty : Request.QueryString["shopid"].ToString();
                //获取模式 （ 编辑or删除or添加 ）
                mode = String.IsNullOrEmpty(Request.QueryString["mode"].ToString()) ? String.Empty : Request.QueryString["mode"].ToString();

                //获取商品编号
                ProductID = String.IsNullOrEmpty(Request.Params["Productid"]) ? String.Empty : Request.Params["Productid"];
            }
            else
            {
                if (TempData["ShopID"] != null)
                {
                    shopid = TempData["ShopID"].ToString();

                }
                if (TempData["productID"] != null)
                {
                    ProductID = TempData["productID"].ToString();
                }
            }

            Control_EditShop_Main_Init(shopid, mode, ProductID);
            return View();
        }

        private void Control_EditShop_Main_Init(String shopid, String mode, String ProductID)
        {

            Int32 intshopid = String.IsNullOrEmpty(shopid) ? 0 : Int32.Parse(shopid);

            Trademark_GetShop(intshopid);

            Trademark_GetShopInfo(intshopid);

            Trademark_GetShopImage(intshopid);

            Trademark_GetShopPosition(intshopid);

            GetShopImageList(intshopid);

            ViewData["ShopCategory"] = new SelectList(m_dalCom.GetShopCategory(), "categoryID", "categoryName");

            //获取商品列表
            GetProductList(intshopid);

            switch (mode)
            {
                case "edit":
                    if (!String.IsNullOrEmpty(ProductID))
                    {
                        ViewData["ShowProductEdit"] = "block";
                        ViewData["ShowProductAdd"] = "none";
                        Int32 intproductid = 0;
                        Int32.TryParse(ProductID, out intproductid);
                        GetProduct(intshopid, intproductid);
                        GetProductImage(intproductid);
                    }
                    else
                    {
                        ViewData["ShowProductEdit"] = "none";
                        ViewData["ShowProductAdd"] = "none";
                    }
                    break;
                case "add":
                    ViewData["ShowProductAdd"] = "block";
                    ViewData["ShowProductEdit"] = "none";
                    break;
                default:
                    ViewData["ShowProductAdd"] = "none";
                    ViewData["ShowProductEdit"] = "none";
                    break;

            }
        }

        #endregion

        #region 公共方法：编辑商铺

        public ActionResult EditShop(HttpPostedFileBase Image)
        {
            Control_EditShop_Main_Init(Request.Params["shopid"], "edit", "");
            if (String.IsNullOrEmpty(Request.Params["CHName"]))
            {
                ViewData.ModelState.AddModelError("VMCHName", "商铺名称不可为空。");
            }
            else
            {
                ViewData["Name"] = Request.Params["CHName"];
            }
            if (String.IsNullOrEmpty(Request.Params["JPName"]))
            {
                ViewData.ModelState.AddModelError("VMJPName", "日文名称不可为空。");
            }
            else
            {
                ViewData["V02"] = Request.Params["JPName"];
            }
            if (String.IsNullOrEmpty(Request.Params["ENName"]))
            {
                ViewData.ModelState.AddModelError("VMENName", "英文名称不可为空。");
            }
            else
            {
                ViewData["V03"] = Request.Params["ENName"];
            }
            //if (String.IsNullOrEmpty(Request.Params["Category"])) { ViewData.ModelState.AddModelError("VMCategory", "类别不可为空。"); } else { ViewData["V03"] = Request.Params["Category"]; }
            if (String.IsNullOrEmpty(Request.Params["Web"]))
            {
                ViewData.ModelState.AddModelError("VMWeb", "官方网站不可为空。");
            }
            else
            {
                ViewData["V05"] = Request.Params["Web"];
            }
            if (String.IsNullOrEmpty(Request.Params["Traffic"]))
            {
                ViewData.ModelState.AddModelError("VMTraffic", "门票不可为空。");
            }
            else
            {
                ViewData["V10"] = Request.Params["Traffic"];
            }
            if (String.IsNullOrEmpty(Request.Params["Hours"]))
            {
                ViewData.ModelState.AddModelError("VMHours", "营业时间不可为空。");
            }
            else
            {
                ViewData["V07"] = Request.Params["Hours"];
            }
            if (String.IsNullOrEmpty(Request.Params["Phone"]))
            {
                ViewData.ModelState.AddModelError("VMPhone", "联系电话不可为空。");
            }
            else
            {
                ViewData["V08"] = Request.Params["Phone"];
            }
            if (String.IsNullOrEmpty(Request.Params["Descrition"]))
            {
                ViewData.ModelState.AddModelError("VMDescrition", "基本介绍不可为空。");
            }
            else
            {
                ViewData["Descrition"] = Request.Params["Descrition"];
            }
            if (String.IsNullOrEmpty(Request.Params["Tickets"]))
            {
                ViewData.ModelState.AddModelError("VMTickets", "门票不可为空。");
            }
            else
            {
                ViewData["V10"] = Request.Params["Tickets"];
            }
            if (String.IsNullOrEmpty(Request.Params["Tel"]))
            {
                ViewData.ModelState.AddModelError("VMTel", "定位电话不可为空。");
            }
            else
            {
                ViewData["V11"] = Request.Params["Tel"];
            }
            if (String.IsNullOrEmpty(Request.Params["Fax"]))
            {
                ViewData.ModelState.AddModelError("VMFax", "传真不可为空。");
            }
            else
            {
                ViewData["V12"] = Request.Params["Fax"];
            }
            if (String.IsNullOrEmpty(Request.Params["Holiday"]))
            {
                ViewData.ModelState.AddModelError("VMHoliday", "休息日不可为空。");
            }
            else
            {
                ViewData["V13"] = Request.Params["Holiday"];
            }
            if (String.IsNullOrEmpty(Request.Params["Special"]))
            {
                ViewData.ModelState.AddModelError("VMSpecial", "特别设施不可为空。");
            }
            else
            {
                ViewData["V14"] = Request.Params["Special"];
            }
            if (String.IsNullOrEmpty(Request.Params["Seats"]))
            {
                ViewData.ModelState.AddModelError("VMSeats", "席位不可为空。");
            }
            else
            {
                ViewData["V15"] = Request.Params["Seats"];
            }
            if (String.IsNullOrEmpty(Request.Params["Language"]))
            {
                ViewData.ModelState.AddModelError("VMLanguage", "语言不可为空。");
            }
            else
            {
                ViewData["V16"] = Request.Params["Language"];
            }
            if (String.IsNullOrEmpty(Request.Params["CreditCard"]))
            {
                ViewData.ModelState.AddModelError("VMCreditCard", "信用卡不可为空。");
            }
            else
            {
                ViewData["V17"] = Request.Params["CreditCard"];
            }
            if (String.IsNullOrEmpty(Request.Params["Average"]))
            {
                ViewData.ModelState.AddModelError("VMAveragee", "平均消费不可为空。");
            }
            else
            {
                ViewData["V18"] = Request.Params["Average"];
            }
            if (String.IsNullOrEmpty(Request.Params["GPS"]))
            {
                ViewData.ModelState.AddModelError("VMGPS", "定位地址不可为空。");
            }
            else
            {
                ViewData["GPS"] = Request.Params["GPS"];
            }
            if (!ViewData.ModelState.IsValid)
            {

                return View("Control_EditShop_Main");
            }

            Int32 intshopid = 0;
            Int32.TryParse(Request.Params["shopid"], out intshopid);
            Int32 intimageid = 0;
            Int32.TryParse(Request.Params["ImageID"], out intimageid);
            update_EditShop(intshopid);
            UpdateShopImage(intshopid, Image, intimageid);
            TempData["ShopID"] = Request.Params["shopid"];
            return RedirectToAction("Control_EditShop_Main");
        }

        #endregion

        #region 公共方法：编辑产品

        public ActionResult Control_EditProduct(HttpPostedFileBase Image)
        {

            if (!String.IsNullOrEmpty(Request.Params["ShopID"]) && !String.IsNullOrEmpty(Request.Params["productID"]) && !String.IsNullOrEmpty(Request.Params["ImageIdPro"]))
            {
                int intshopid = 0;
                int intproductid = 0;
                int intimageid = 0;
                Int32.TryParse(Request.Params["ShopID"], out intshopid);
                Int32.TryParse(Request.Params["productID"], out intproductid);
                Int32.TryParse(Request.Params["ImageIdPro"], out intimageid);

                TempData["ShopID"] = Request.Params["ShopID"];

                if (String.IsNullOrEmpty(Request.Params["Descrition"]))
                {
                    ViewData.ModelState.AddModelError("VMDescrition", "基本介绍不可为空");
                }
                if (String.IsNullOrEmpty(Request.Params["ProductName"]))
                {
                    ViewData.ModelState.AddModelError("VMProductName", "商品名称不可为空");
                }
                if (String.IsNullOrEmpty(Request.Params["Grade"]))
                {
                    ViewData.ModelState.AddModelError("VMGrade", "品级不可为空");
                }
                //if (String.IsNullOrEmpty(Request.Params["Address"]))
                //{
                //    ViewData.ModelState.AddModelError("Address", "地址不可为空");
                //}
                if (String.IsNullOrEmpty(Request.Params["Price"]))
                {
                    ViewData.ModelState.AddModelError("VMPrice", "价格不可为空");
                }
                if (String.IsNullOrEmpty(Request.Params["ProductType"]))
                {
                    ViewData.ModelState.AddModelError("VMProductType", "商品类型不可为空");
                }
                //if (Image == null)
                //{
                //    ViewData.ModelState.AddModelError("VMImage", "图片不可为空");
                //}
                if (String.IsNullOrEmpty(Request.Params["ImageAlt"]))
                {
                    ViewData.ModelState.AddModelError("VMImageAlt", "图片描述不可为空");
                }
                if (!ViewData.ModelState.IsValid)
                {
                    Control_EditShop_Main_Init(Request.Params["ShopID"], "edit", Request.Params["productID"]);
                    return View("Control_EditShop_Main");
                }

                EditProduct(intshopid, intproductid);
                UpdateProductImage(intshopid, intproductid, Image, intimageid, Request.Params["ImageAlt"]);
                TempData["ProductID"] = Request.Params["productID"];
                return RedirectToAction("Control_EditShop_Main");
            }
            return View();

        }

        #endregion

        #region 公共方法：添加子商铺

        public ActionResult Control_AddSubShop()
        {
            if (Request.QueryString["shopid"] != null)
            {
                ViewData["ShopID"] = Request.QueryString["shopid"];
            }
            else
            {
                return RedirectToAction("Control");
            }

            return View();
        }

        public ActionResult Register_AddSubShop(HttpPostedFileBase Image)
        {

            //if (!Regex.Equals(Request.Params["ShopName"].Trim(), @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"))
            if (String.IsNullOrEmpty(Request.Params["ShopName"].Trim()))
            {
                ViewData.ModelState.AddModelError("VMShopName", "子店名称不可为空。");
            }

            if (String.IsNullOrEmpty(Request.Params["Address"].Trim()))
            {
                ViewData.ModelState.AddModelError("VMAddress", "子店地址不可为空。");
            }

            //if (Image == null)
            //{
            //    ViewData.ModelState.AddModelError("VMImage", "商户图片不可为空。");
            //}

            if (String.IsNullOrEmpty(Request.Params["ImageAlt"].Trim()))
            {
                ViewData.ModelState.AddModelError("VMImageAlt", "图片描述不可为空。");
            }

            if (String.IsNullOrEmpty(Request.Params["Tel"].Trim()))
            {
                ViewData.ModelState.AddModelError("VMTel", "子店电话不可为空。");
            }

            if (String.IsNullOrEmpty(Request.Params["Web"].Trim()))
            {
                ViewData.ModelState.AddModelError("VMWeb", "官方网址不可为空。");
            }

            if (String.IsNullOrEmpty(Request.Params["Descrition"].Trim()))
            {
                ViewData.ModelState.AddModelError("VMDescrition", "商户介绍不可为空。");
            }

            if (!ModelState.IsValid)
            {
                ViewData["ShopName"] = Request.Params["ShopName"];
                ViewData["Address"] = Request.Params["Address"];
                //ViewData["Image"] = Image;
                ViewData["ImageAlt"] = Request.Params["ImageAlt"];
                ViewData["Tel"] = Request.Params["Tel"];
                ViewData["Web"] = Request.Params["Web"];
                ViewData["Descrition"] = Request.Params["Descrition"];
                return View("Control_AddSubShop");
                // return RedirectToAction("Control_AddSubShop", new { shopid = Request.Params["ShopID"] });
            }

            //获取的当前用户编号
            MembershipUser user;
            user = Membership.GetUser(User.Identity.Name);
            Guid UserID = (Guid)user.ProviderUserKey;

            Int32 ParentShopID = 0;
            if (Request.Params["ShopID"] != null)
            {
                Int32.TryParse(Request.Params["ShopID"].ToString(), out ParentShopID);
            }
            else
            {
                return RedirectToAction("Control");
            }

            //新建商铺
            int? inShopID = Control_AddSubShop(UserID, ParentShopID);

            //新建失败处理
            if (inShopID == 0) { return Register(); }

            //新建商铺Logo
            Control_AddShop_InsertShopImage(inShopID.Value, Image, Request.Params["ImageAlt"]);

            //转向商铺列表
            return RedirectToAction("Control");

        }

        #region 私有方法：添加子商铺

        #region 私有方法：新建商铺，商铺位置

        /// <summary>
        /// 新建商铺，商铺位置
        /// </summary>
        /// <returns></returns>
        private int? Control_AddSubShop(Guid userid, Int32 ParentShopID)
        {
            int? rtnShopID = 0;

            //初始化TblDataContext对象
            m_dcTbl = new TblDataContext();

            //初始化tbl_shop对象
            tbl_shop oShop = new tbl_shop();
            oShop.name = Request.Params["ShopName"];
            oShop.address = Request.Params["Address"];
            oShop.description = Request.Params["Descrition"];
            oShop.parentShopID = ParentShopID;
            oShop.createDate = DateTime.Now;

            //初始化tbl_shopPosition对象
            tbl_shopPosition oPosition = new tbl_shopPosition();
            oPosition.address = Request.Params["GPS"];

            //m_dcTbl.prc_shopCreateShop(null,
            //                            oShop.name,
            //                            oPosition.country,
            //                            oPosition.province,
            //                            oPosition.city,
            //                            oPosition.suburb,
            //                            oPosition.street,
            //                            oPosition.postcode,
            //                            oPosition.address,
            //                            oShop.address,
            //                            oPosition.latitude,
            //                            oPosition.longitude,
            //                            oShop.parentShopID,
            //                            oShop.locatedParentShopID,
            //                            oShop.description,
            //                            userid,
            //                            ref rtnShopID);

            return rtnShopID;
        }

        #endregion

        #region 私有方法：新建商铺Logo

        /// <summary>
        /// 新建商品Logo
        /// </summary>
        /// <param name="intShopID"></param>
        /// <param name="Image"></param>
        private void Control_AddShop_InsertShopImage(int intShopID, HttpPostedFileBase Image, string alt)
        {
            if (Image == null) { return; }

            m_dcTbl = new TblDataContext();

            //生成图片名
            String strImageFileName = String.Format("{0}.{1}", intShopID, Image.FileName.Split('.').Last());

            //生成图片路径
            String strPath = String.Format("{0}Content\\img\\TrademarkLogo\\{1}",
                                            AppDomain.CurrentDomain.BaseDirectory,
                                            strImageFileName);

            //保存图片
            Image.SaveAs(strPath);

            //初始化tbl_shopImage对象
            tbl_shopImage oImage = new tbl_shopImage();
            oImage.description = alt;
            oImage.filename = strImageFileName;
            oImage.shopID = intShopID;
            oImage.type = 1;

            m_dcTbl.tbl_shopImage.InsertOnSubmit(oImage);

            //执行事务
            m_dcTbl.SubmitChanges();
        }

        #endregion

        #endregion

        #endregion

        #region 公共方法：添加商品

        public ActionResult Control_AddProduct(HttpPostedFileBase ImageAdd)
        {
            Int32 intShopID = 0;
            Int32 productid = 0;

            if (!String.IsNullOrEmpty(Request.Params["ShopIDAdd"]))
            {
                Int32.TryParse(Request.Params["ShopIDAdd"], out intShopID);
            }
            else
            {
                //错误返回
            }

            if (String.IsNullOrEmpty(Request.Params["ProductNameAdd"].Trim()))
            {
                ViewData.ModelState.AddModelError("VMProductNameAdd", "商品名称不可为空。");
            }
            //else if (!Regex.Equals(Request.Params["ProductNameAdd"].Trim(), @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"))
            //{
            //    ViewData.ModelState.AddModelError("VMProductNameAdd", "商品名称不合法。");
            //}
            if (String.IsNullOrEmpty(Request.Params["ProductTypeAdd"].Trim()))
            {
                ViewData.ModelState.AddModelError("VMProductTypeAdd", "商品类型不可为空。");
            }
            //if (Image == null)
            //{
            //    ViewData.ModelState.AddModelError("VMImage", "商户图片不可为空。");
            //}
            if (String.IsNullOrEmpty(Request.Params["PriceAdd"].Trim()))
            {
                ViewData.ModelState.AddModelError("VMPriceAdd", "价格不可为空。");
            }
            //else if (!Regex.Equals(Request.Params["PriceAdd"].Trim(),@"^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$"))
            //{
            //    ViewData.ModelState.AddModelError("VMPriceAdd", "价格不合法。");
            //}

            if (String.IsNullOrEmpty(Request.Params["GradeAdd"].Trim()))
            {
                ViewData.ModelState.AddModelError("VMGradeAdd", "品级不可为空。");
            }

            if (String.IsNullOrEmpty(Request.Params["ImageAltAdd"].Trim()))
            {
                ViewData.ModelState.AddModelError("VMImageAltAdd", "图片描述不可为空。");
            }

            if (String.IsNullOrEmpty(Request.Params["DescritionAdd"].Trim()))
            {
                ViewData.ModelState.AddModelError("VMDescritionAdd", "基本介绍不可为空。");
            }

            if (!ModelState.IsValid)
            {
                ViewData["ProductNameAdd"] = Request.Params["ProductNameAdd"];
                ViewData["ProductTypeAdd"] = Request.Params["ProductTypeAdd"];
                ViewData["PriceAdd"] = Request.Params["PriceAdd"];
                ViewData["GradeAdd"] = Request.Params["GradeAdd"];
                ViewData["ImageAltAdd"] = Request.Params["ImageAltAdd"];
                ViewData["DescritionAdd"] = Request.Params["DescritionAdd"];
                Control_EditShop_Main_Init(Request.Params["ShopIDAdd"], "add", String.Empty);
                return View("Control_EditShop_Main");
                // return RedirectToAction("Control_AddSubShop", new { shopid = Request.Params["ShopID"] });
            }


            //新建商品信息
            productid = Control_AddProduct_InsertProductInfo(intShopID);

            Control_AddProduct_InsertProductImage(intShopID, productid, ImageAdd, Request.Params["ImageAltAdd"]);

            TempData["ShopID"] = Request.Params["ShopIDAdd"];
            return RedirectToAction("Control_EditShop_Main");
        }

        #region 私有方法：添加商品

        /// <summary>
        /// 添加商品详情
        /// </summary>
        /// <param name="Shopid"></param>
        private Int32 Control_AddProduct_InsertProductInfo(Int32 ShopID)
        {
            m_dcTbl = new TblDataContext();
            tbl_shopProduct product = new tbl_shopProduct();
            product.productID = 8;
            product.shopID = ShopID;
            product.description = Request.Params["DescritionAdd"];
            product.productName = Request.Params["ProductNameAdd"];
            if (!String.IsNullOrEmpty(Request.Params["GradeAdd"]))
            {
                Int32 temp = 0;
                Int32.TryParse(Request.Params["GradeAdd"], out temp);
                product.grade = temp;
            }
            if (!String.IsNullOrEmpty(Request.Params["ProductTypeAdd"]))
            {
                Int32 temp = 0;
                Int32.TryParse(Request.Params["ProductTypeAdd"], out temp);
                product.productType = temp;
            }

            if (!String.IsNullOrEmpty(Request.Params["PriceAdd"]))
            {
                Int32 temp = 0;
                Int32.TryParse(Request.Params["PriceAdd"], out temp);
                product.price = temp;
            }
            product.Language = String.Empty;
            product.oppose = 0;
            product.suppose = 0;
            product.views = 0;
            m_dcTbl.tbl_shopProduct.InsertOnSubmit(product);
            m_dcTbl.SubmitChanges();
            return product.productID;
        }

        /// <summary>
        /// 添加商品图片
        /// </summary>
        /// <param name="Shopid"></param>
        /// <param name="ProductID"></param>
        /// <param name="ImageAdd"></param>
        private void Control_AddProduct_InsertProductImage(Int32 ShopID, Int32 ProductID, HttpPostedFileBase ImageAdd, String alt)
        {
            m_dcTbl = new TblDataContext();
            tbl_shopImage oImage = new tbl_shopImage();

            oImage.productID = ProductID;
            oImage.type = 2;
            oImage.shopID = ShopID;
            oImage.filename = "";
            oImage.description = alt;

            if (ImageAdd != null)
            {
                //生成图片名
                String strImageFileName = String.Format("{0}.{1}", ShopID, ImageAdd.FileName.Split('.').Last());

                //生成图片路径
                String strPath = String.Format("{0}Content\\img\\TrademarkLogo\\{1}",
                                                AppDomain.CurrentDomain.BaseDirectory,
                                                strImageFileName);

                //保存图片
                ImageAdd.SaveAs(strPath);

            }

            m_dcTbl.tbl_shopImage.InsertOnSubmit(oImage);

            //执行事务
            m_dcTbl.SubmitChanges();

        }

        #endregion

        #endregion

        #region 私有方法：获取商铺信息

        /// <summary>
        /// 查询商铺位置
        /// </summary>
        /// <param name="intShopID"></param>
        private void Trademark_GetShopPosition(Int32 intShopID)
        {
            IEnumerable<tbl_shopPosition> oPosition = from p in m_dcTbl.tbl_shopPosition
                                                      from s in m_dcTbl.tbl_shop
                                                      where p.positionID == s.positionID
                                                      where s.shopID == intShopID
                                                      select p;
            if (oPosition.Count() != 0)
            {
                ViewData["GPS"] = oPosition.First().address;
            }
            else
            {
                //ViewData["GPS"] = oPosition.First().address;
            }
        }

        /// <summary>
        /// 查询商铺详细信息
        /// </summary>
        /// <param name="intShopID"></param>
        private void Trademark_GetShopInfo(Int32 intShopID)
        {
            //获得tbl_shopCategoryMappingValue对象集合
            IEnumerable<tbl_shopCategoryMappingValue> oValues = from o in m_dcTbl.tbl_shopCategoryMappingValue
                                                                where o.shopID == intShopID
                                                                select o;
            //赋值ViewData
            foreach (tbl_shopCategoryMappingValue oValue in oValues)
            {
                ViewData["V" + oValue.itemID.ToString("D2")] = oValue.value;
            }
        }

        /// <summary>
        /// 查询商铺Logo
        /// </summary>
        /// <param name="intShopID"></param>
        private void Trademark_GetShopImage(Int32 intShopID)
        {
            IEnumerable<tbl_shopImage> oImages = from o in m_dcTbl.tbl_shopImage
                                                 where o.shopID == intShopID
                                                 where o.type == 1
                                                 select o;

            if (oImages.Count() == 0)
            {
                ViewData["Logo"] = String.Empty;
                ViewData["LogoAlt"] = String.Empty;
                ViewData["ImageID"] = String.Empty;
            }
            else
            {
                ViewData["Logo"] = oImages.First().filename;
                ViewData["LogoAlt"] = oImages.First().description;
                ViewData["ImageID"] = oImages.First().imageID;
            }
        }

        /// <summary>
        /// 获取商铺图片列表
        /// </summary>
        /// <param name="intShopID"></param>
        private void GetShopImageList(Int32 intShopID)
        {
            var ShopImage = new TblDataContext();
            var ShopImageList = from s in ShopImage.tbl_shopImage
                                where s.shopID == intShopID
                                where s.type == 3
                                select s;
            ViewData["ShopImages"] = ShopImageList.ToList<tbl_shopImage>();
        }

        #endregion

        #region 私有方法：获取商品信息

        /// <summary>
        /// 获取商品列表
        /// </summary>
        /// <param name="intShopID"></param>
        private void GetProductList(Int32 intShopID)
        {
            var Product = new TblDataContext();
            var ProductList = from s in Product.tbl_shopProduct
                              where s.shopID == intShopID
                              select s;

            ViewData["ProductList"] = ProductList.ToList<tbl_shopProduct>();
        }



        /// <summary>
        /// 获取商品信息
        /// </summary>
        /// <param name="intShopID"></param>
        /// <param name="productID"></param>
        private void GetProduct(Int32 intShopID, Int32 productID)
        {
            var Product = new TblDataContext();
            var ProductList = from s in Product.tbl_shopProduct
                              where s.shopID == intShopID
                              where s.productID == productID
                              select s;

            ViewData["ProductInfo"] = ProductList.First();
        }

        /// <summary>
        /// 获取图片
        /// </summary>
        /// <param name="productID"></param>
        private void GetProductImage(Int32 productID)
        {
            var shopimage = new TblDataContext();
            var product = from p in shopimage.tbl_shopImage
                          where p.productID == productID
                          select p;

            if (product.Count() != 0)
            {
                ViewData["Image"] = product.First().filename;
                ViewData["ImageD"] = product.First().description;
                ViewData["ImageID"] = product.First().imageID;
            }
        }

        #endregion

        #region 私有方法：更新商铺信息

        private void update_EditShop(Int32 intShopID)
        {
            //初始化TblDataContext对象
            m_dcTbl = new TblDataContext();

            var shop = from p in m_dcTbl.tbl_shop
                       where p.shopID == intShopID
                       select p;
            if (shop.Count() != 0)
            {
                var shopfrist = shop.First();

                shopfrist.address = Request.Params["Address"];
                shopfrist.description = Request.Params["Descrition"];
                shopfrist.name = Request.Params["ShopName"];


                var shopCategoryMappingValue = from v in m_dcTbl.tbl_shopCategoryMappingValue
                                               where v.shopID == intShopID
                                               select v;
                if (shopCategoryMappingValue.Count() != 0)
                {
                    var values = shopCategoryMappingValue.ToList();

                    foreach (tbl_shopCategoryMappingValue v in values)
                    {
                        switch (v.itemID)
                        {
                            case 1:
                                v.value = String.IsNullOrEmpty(Request.Params["CHName"]) ? " " : Request.Params["CHName"]; break;
                            case 2:
                                v.value = String.IsNullOrEmpty(Request.Params["JPName"]) ? " " : Request.Params["JPName"]; break;
                            case 3:
                                v.value = String.IsNullOrEmpty(Request.Params["ENName"]) ? " " : Request.Params["ENName"]; break;
                            case 4:
                                v.value = String.IsNullOrEmpty(Request.Params["Category"]) ? " " : Request.Params["Category"]; break;
                            case 5:
                                v.value = String.IsNullOrEmpty(Request.Params["Web"]) ? " " : Request.Params["Web"]; break;
                            case 6:
                                v.value = String.IsNullOrEmpty(Request.Params["Traffic"]) ? " " : Request.Params["Traffic"]; break;
                            case 7:
                                v.value = String.IsNullOrEmpty(Request.Params["Hours"]) ? " " : Request.Params["Hours"]; break;
                            case 8:
                                v.value = String.IsNullOrEmpty(Request.Params["Phone"]) ? " " : Request.Params["Phone"]; break;
                            case 9:
                                v.value = String.IsNullOrEmpty(Request.Params["Descrition"]) ? " " : Request.Params["Descrition"]; break;
                            case 10:
                                v.value = String.IsNullOrEmpty(Request.Params["Tickets"]) ? " " : Request.Params["Tickets"]; break;
                            case 11:
                                v.value = String.IsNullOrEmpty(Request.Params["Tel"]) ? " " : Request.Params["Tel"]; break;
                            case 12:
                                v.value = String.IsNullOrEmpty(Request.Params["Fax"]) ? " " : Request.Params["Fax"]; break;
                            case 13:
                                v.value = String.IsNullOrEmpty(Request.Params["Holiday"]) ? " " : Request.Params["Holiday"]; break;
                            case 14:
                                v.value = String.IsNullOrEmpty(Request.Params["Special"]) ? " " : Request.Params["Special"]; break;
                            case 15:
                                v.value = String.IsNullOrEmpty(Request.Params["Seats"]) ? " " : Request.Params["Seats"]; break;
                            case 16:
                                v.value = String.IsNullOrEmpty(Request.Params["Language"]) ? " " : Request.Params["Language"]; break;
                            case 17:
                                v.value = String.IsNullOrEmpty(Request.Params["CreditCard"]) ? " " : Request.Params["CreditCard"]; break;
                            case 18:
                                v.value = String.IsNullOrEmpty(Request.Params["Average"]) ? " " : Request.Params["Average"]; break;
                            case 19:
                                v.value = String.IsNullOrEmpty(Request.Params["GPS"]) ? " " : Request.Params["GPS"]; break;
                            default: break;
                        }
                    }

                    m_dcTbl.SubmitChanges();
                }
                else
                {
                    Int32 intCategory = 0;
                    Int32.TryParse(Request.Params["Category"], out intCategory);

                    //初始化tbl_shopCategoryMappingValue对象集
                    List<tbl_shopCategoryMappingValue> oValue = new List<tbl_shopCategoryMappingValue>();
                    for (int i = 0; i < 19; i++) { oValue.Add(new tbl_shopCategoryMappingValue()); }
                    oValue[0].value = Request.Params["CHName"];
                    oValue[1].value = Request.Params["JPName"];
                    oValue[2].value = Request.Params["ENName"];
                    oValue[3].value = Request.Params["Category"];
                    oValue[4].value = Request.Params["Web"];
                    oValue[5].value = Request.Params["Traffic"];
                    oValue[6].value = Request.Params["Hours"];
                    oValue[7].value = Request.Params["Phone"];
                    oValue[8].value = Request.Params["Descrition"];
                    oValue[9].value = Request.Params["Tickets"];
                    oValue[10].value = Request.Params["Tel"];
                    oValue[11].value = Request.Params["Fax"];
                    oValue[12].value = Request.Params["Holiday"];
                    oValue[13].value = Request.Params["Special"];
                    oValue[14].value = Request.Params["Seats"];
                    oValue[15].value = Request.Params["Language"];
                    oValue[16].value = Request.Params["CreditCard"];
                    oValue[17].value = Request.Params["Average"];
                    oValue[18].value = Request.Params["GPS"];
                    for (Int32 i = 0; i < oValue.Count; i++)
                    {
                        oValue[i].categoryID = intCategory;
                        oValue[i].itemID = i + 1;
                        oValue[i].shopID = intShopID;
                        m_dcTbl.tbl_shopCategoryMappingValue.InsertOnSubmit(oValue[i]);
                    }

                    //初始化tbl_shopCategoryMapping对象
                    tbl_shopCategoryMapping oCateMapp = new tbl_shopCategoryMapping();
                    oCateMapp.shopID = intShopID;
                    oCateMapp.categoryID = intCategory;
                    m_dcTbl.tbl_shopCategoryMapping.InsertOnSubmit(oCateMapp);

                    //执行事务
                    m_dcTbl.SubmitChanges();
                }
            }
        }

        #endregion

        #region 私有方法：更新产品信息

        private void EditProduct(Int32 intShopID, Int32 intproductID)
        {
            var Product = new TblDataContext();
            var ProductList = from s in Product.tbl_shopProduct
                              where s.shopID == intShopID
                              where s.productID == intproductID
                              select s;
            if (ProductList.Count() != 0)
            {
                var Productfrist = ProductList.First();

                Productfrist.description = Request.Params["Descrition"];
                //Productfrist.Language = Request.Params["ShopName"];
                Productfrist.productName = Request.Params["ProductName"];
                if (!String.IsNullOrEmpty(Request.Params["Grade"]))
                {
                    int temp = 0;
                    Int32.TryParse(Request.Params["Grade"], out temp);
                    Productfrist.grade = temp;
                }

                if (!String.IsNullOrEmpty(Request.Params["Address"]))
                {
                    int temp = 0;
                    Int32.TryParse(Request.Params["Address"], out temp);
                    Productfrist.oppose = temp;
                }
                if (!String.IsNullOrEmpty(Request.Params["Price"]))
                {
                    int temp = 0;
                    Int32.TryParse(Request.Params["Price"], out temp);
                    Productfrist.price = temp;
                }
                if (!String.IsNullOrEmpty(Request.Params["ProductType"]))
                {
                    int temp = 0;
                    Int32.TryParse(Request.Params["ProductType"], out temp);
                    Productfrist.productType = temp;
                }
                if (!String.IsNullOrEmpty(Request.Params["Descrition"]))
                {
                    int temp = 0;
                    Int32.TryParse(Request.Params["Descrition"], out temp);
                    Productfrist.productType = temp;
                }
                //if (String.IsNullOrEmpty(Request.Params[""]))
                //{
                //    int temp = 0;
                //    Int32.TryParse(Request.Params[""], out temp);
                //    Productfrist.views = temp;
                //}
                //if (String.IsNullOrEmpty(Request.Params[""]))
                //{
                //    int temp = 0;
                //    Int32.TryParse(Request.Params[""], out temp);
                //    Productfrist.suppose = temp;
                //}

                Product.SubmitChanges();
            }
        }

        #endregion

        #region 私有方法：更新商铺图片信息

        private void UpdateShopImage(int intShopID, HttpPostedFileBase Image, int ImageID)
        {
            if (ImageID == 0)
            {
                Register_AddShop_InsertShopImage(intShopID, Image);
                return;
            }

            if (Image == null) { return; }

            m_dcTbl = new TblDataContext();

            //生成图片名
            String strImageFileName = String.Format("{0}.{1}", intShopID, Image.FileName.Split('.').Last());

            //生成图片路径
            String strPath = String.Format("{0}Content\\img\\TrademarkLogo\\{1}",
                                            AppDomain.CurrentDomain.BaseDirectory,
                                            strImageFileName);

            //保存图片
            Image.SaveAs(strPath);

            try
            {
                //初始化tbl_shopImage对象
                tbl_shopImage oImage = m_dcTbl.tbl_shopImage.First(c => c.imageID == ImageID);
                if (oImage != null)
                {
                    oImage.description = Request.Params["ImageAlt"];
                    oImage.filename = strImageFileName;
                    //oImage.shopID = intShopID;
                    //执行事务
                    m_dcTbl.SubmitChanges();
                }
            }
            catch
            { }

        }

        #endregion

        #region 私有方法：更新商品图片信息

        private void UpdateProductImage(int intShopID, int iniproductID, HttpPostedFileBase Image, int ImageID, String alt)
        {
            if (ImageID == 0)
            {
                Control_AddProduct_InsertProductImage(intShopID, iniproductID, Image, alt);
                return;
            }

            if (Image == null) { return; }

            m_dcTbl = new TblDataContext();

            //生成图片名
            String strImageFileName = String.Format("{0}.{1}", intShopID, Image.FileName.Split('.').Last());

            //生成图片路径
            String strPath = String.Format("{0}Content\\img\\TrademarkLogo\\{1}",
                                            AppDomain.CurrentDomain.BaseDirectory,
                                            strImageFileName);

            //保存图片
            Image.SaveAs(strPath);

            try
            {

                //初始化tbl_shopImage对象
                tbl_shopImage oImage = m_dcTbl.tbl_shopImage.First(c => c.imageID == ImageID);
                if (oImage != null)
                {
                    oImage.description = alt;
                    oImage.filename = strImageFileName;
                    //oImage.shopID = intShopID;
                    //执行事务
                    m_dcTbl.SubmitChanges();
                }
            }
            catch
            { }

        }

        #endregion

        public ActionResult Control_ShopList()
        {

            return View();
        }

        #endregion

        #region 公共方法：添加商铺图片

        public ActionResult EditShopImage(HttpPostedFileBase ShopImage, String ShopImageAlt)
        {
            Int32 intshopid = 0;
            Int32.TryParse(Request.Params["shopid"], out intshopid);
            EditShopImage_AddImage(intshopid, ShopImage, ShopImageAlt, "81");
            TempData["ShopID"] = intshopid.ToString();
            Control_EditShop_Main_Init(intshopid.ToString(), string.Empty, string.Empty);
            return RedirectToAction("Control_EditShop_Main");
        }

        private void EditShopImage_AddImage(int intShopID, HttpPostedFileBase Image, String ShopImageAlt, String CountryID)
        {
            if (Image == null) { return; }

            m_dcTbl = new TblDataContext();

            //生成图片名
            String strImageFileName = String.Format("{0}.{1}", intShopID, Image.FileName.Split('.').Last());

            //生成图片路径
            String strPath = String.Format("{0}Content\\img\\shop\\{1}\\{2}",
                                            AppDomain.CurrentDomain.BaseDirectory,
                                            CountryID,
                                            intShopID.ToString(),
                                            strImageFileName);

            //保存图片
            Image.SaveAs(strPath);

            //初始化tbl_shopImage对象
            tbl_shopImage oImage = new tbl_shopImage();
            oImage.description = ShopImageAlt;
            oImage.filename = strImageFileName;
            oImage.shopID = intShopID;
            oImage.type = 3;

            m_dcTbl.tbl_shopImage.InsertOnSubmit(oImage);

            //执行事务
            m_dcTbl.SubmitChanges();
        }


        #endregion

    }
}
